package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }







    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime){
        log.info("接收参数: page = {}, pageSize = {}, number = {}, beginTime = {}, endTime = {}",page,pageSize,number,beginTime,endTime);

        Page<Orders> pageInfo = new Page<>();

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        ordersLambdaQueryWrapper.between(beginTime != null && endTime != null, Orders::getOrderTime,beginTime,endTime);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo,ordersLambdaQueryWrapper);

        return R.success(pageInfo);
    }


    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("操作成功");
    }


    @GetMapping("/userPage")
    public R<Page> findOrderPage(int page , int pageSize){
        Page<Orders> pageInfo = new Page<>();

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());

        Page<Orders> ordersPage = orderService.page(pageInfo, ordersLambdaQueryWrapper);


        Page<OrderDto> orderDtoPage = new Page<>();
        orderDtoPage.setTotal(ordersPage.getTotal());

        List<Orders> records = ordersPage.getRecords();
        List<OrderDto> dtoList = records.stream().map(orders -> {
            OrderDto dto = new OrderDto();
            BeanUtils.copyProperties(orders, dto);

            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orders.getId());

            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);

            dto.setOrderDetails(orderDetailList);

            return dto;
        }).collect(Collectors.toList());

        orderDtoPage.setRecords(dtoList);

        return R.success(orderDtoPage);
    }
}